package com.filter;

import com.bean.BhUser;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter({"/BhSyDao/Teacher/*","/UserTopic/*"})
public class BhSyAllFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request= (HttpServletRequest) servletRequest;
        HttpServletResponse response= (HttpServletResponse) servletResponse;
        HttpSession session=request.getSession();
        BhUser bh= (BhUser) session.getAttribute("BhSyUser");
        System.out.println(bh);
        System.out.println("权限判断中...");
        if (bh==null){//若获取到的用户对象为null，则跳转到登录页面
            System.out.println("用户未登陆");
            response.sendRedirect("../GoBhSy/openWeb?findU=BhSyCaseJoin.jsp");
            return;
        } else if (bh.uID!=2){
            System.out.println("用户权限无法访问");
            response.sendRedirect("../GoBhSy/openWeb?findU=BhSyCaseJoin.jsp");
            return;
        }
        filterChain.doFilter(servletRequest,servletResponse);

    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("权限过滤器初始化");
    }

    @Override
    public void destroy() {
        System.out.println("权限过滤器销毁");
    }
}
